var myPlayer = {
	valumeMax:64,		//声音最大值
	valumeMin:0,		//声音最小值
	volumeBoxWidth:400,	//声音滑块的宽度
	oldVolume:0,		//上一次声音值
	hasInitVolume:false,	//是否已经存初化声音
	hideVolumeTimer:false,	//自动隐藏声音定时器
	state:'init',          //状态
	isPause:false,
	mute:false,			//静音
	showSeek:true,
	run:function(type){
		if(!player) return false;
		var playerState = player.getPlayerState(); 
		if(playerState != 5 ){
			if(playerState == 2 && type == 'pause'){
					
			}else{
				return false;
			}
		}
		if(type == 'ok'){
			this.showContralBox();
		}else if(type == 'left' || type=='right' || type=='mute'){
			this.volumeShow(type);
		}else if(type == 'seek'){
			if(this.showSeek){
				this.showSeekBox();
			}
		}else if(type == 'pause'){
			this.changePauseOrPlay();
		}
	},
	initPauseOrPlay:function(){
		var a = document.getElementById('pauseOrPlayA');
		a.onfocus = function(){KEY.CURTYPE = 'pauseOrPlay';}
		var c = $.getConfigValue("hidePauseOrPlayTip");
		var pauseorPlayBoxBottomBox = document.getElementById('pauseorPlayBoxBottomBox2');
		if(c){
			this.hidePauseOrPlayTip = true;
			pauseorPlayBoxBottomBox.style.display = 'none';
		}else{
			this.hidePauseOrPlayTip = false;
			pauseorPlayBoxBottomBox.style.display = 'block';
		}
	},
	hidePauseOrPlayTipBox:function(){
		var pauseorPlayBoxBottomBox = document.getElementById('pauseorPlayBoxBottomBox2');
		this.hidePauseOrPlayTip = true;
		pauseorPlayBoxBottomBox.style.display = 'none';
		 $.setConfigValue("hidePauseOrPlayTip","true");
	},
	changePauseOrPlay:function(){
		var pauseOrPlayBox = document.getElementById('pauseOrPlayBox');
		var p_state = player.getPlayerState();
		if(!this.hasInitPauseOrPlay){
			this.initPauseOrPlay();
			this.hasInitPauseOrPlay = true;
		}
		if(p_state == 2){		//当播放器的状态是 apsWait 3 等待,apsBuffer 4缓冲中是显示缓冲进
			pauseOrPlayBox.style.display = 'none';
			player.resumePlay();
			KEY.CURTYPE = false;
		}else{
			
			var pauseorPlayBoxBottomLeft = document.getElementById('pauseorPlayBoxBottomLeft');
			var con = '<div>'+this.disName+'</div>';
			
			var cur = player.getCurTime ();
			var dur = player.getDuration();
			cur = parseInt(cur/1000);
			dur = parseInt(dur/1000);
			con += '<div>'+this.formatPlayTime(cur)+'/'+this.formatPlayTime(dur)+'</div>';
			pauseorPlayBoxBottomLeft.innerHTML = con;
			
			pauseOrPlayBox.style.display = 'block';
			player.pausePlay();
			document.focus('pauseOrPlayA');
		}
	},
	volumeShow:function(type){
		if(!this.hasInitVolume){
			this.initVolumeBox();
			this.hasInitVolume = true;
		}
		if(type == 'mute'){
			this.changeVolumeMute();
			
		}
		this.changeVolumePos();
		var voluemBox = document.getElementById('voluemBox');
		voluemBox.style.display = 'block';
		document.focus('playVolumeA');
		
		var date = new Date();
		KEY.PRESSTIME = date.getTime();		//
		if(this.hideVolumeTimer) {clearInterval(this.hideVolumeTimer);this.hideVolumeTimer = null;}
		this.hideVolumeTimer = setInterval( "myPlayer.hideVolumeAuto()",1*1000);	
	},
	changeVolumeMute:function(){
		var v = player.getVolume();$.log('v:'+v);
		if(v == this.valumeMin){
			player.setVolume(this.oldVolume);
		}else{
			this.oldVolume = v;
			player.setVolume(this.valumeMin);
		}
		
		this.changeVolumePos();
	},
	initVolumeBox:function(){
		var a = document.getElementById('playVolumeA');
		a.onfocus = function(){KEY.CURTYPE = 'volumeBox';}
	},
	changeVolumePos:function(){
		var val = player.getVolume();
		var w = this.getVoluemPosition(val);
		var voluemBoxPos = document.getElementById('voluemBoxPos'); 
		voluemBoxPos.style.width= w;
		
		var volumeBoxValue = document.getElementById('volumeBoxValue');
		volumeBoxValue.innerHTML = val;
		
		var voluemBoxMute = document.getElementById('voluemBoxMute');
		if(val == this.valumeMin){
			voluemBoxMute.className = 'voluemBoxMute0';
		}else{
			voluemBoxMute.className = 'voluemBoxMute1';
		}
	},
	getVoluemPosition:function(val){
		var w = val*this.volumeBoxWidth/(this.valumeMax - this.valumeMin);
		return parseInt(w);
	},
	hideVolumeAuto:function(){
		var date = new Date();
		var curTime = date.getTime();	
		if((curTime - KEY.PRESSTIME) >= 5000){
			this.hideVolume();
		}
	},
	hideVolume:function(){
		if(this.hideVolumeTimer) {clearInterval(this.hideVolumeTimer);this.hideVolumeTimer = null;}
		KEY.CURTYPE = false;
		var voluemBox = document.getElementById('voluemBox');
		voluemBox.style.display = 'none';
	},
	changeVolumeAdd:function(){
		var val = player.getVolume();
		if(val < this.valumeMax){
			val++;
			player.setVolume(val);
			this.changeVolumePos();
		}
	},
	changeVolumeDec:function(){
		var val = player.getVolume();
		if(val > this.valumeMin){
			val--;
			player.setVolume(val);
			this.changeVolumePos();
		}
	},
	
	
	hasInitSeekBox:false,
	playCurTime:0,			//播放的当前时间
	curTime:0,				//记录当前时间
	playTotalTime:100,		//播放的总时长
	seekAddValue:0,
	seekBoxWidth:640,		//缓冲宽度
	hideSeekTimer:null,
	isShowSeek:false,
	showSeekBox:function(bo){
		if(!this.hasInitSeekBox){
			this.initSeekBox();
			this.hasInitSeekBox = true;
		}
		this.isShowSeek = true;
		
		var cur = player.getCurTime ();
		var dur = player.getDuration();
		cur = parseInt(cur/1000);
		dur = parseInt(dur/1000);
		
		this.playTotalTime = dur;
		this.playCurTime = cur;
		this.oldPlayCurTime = cur;
		this.seekAddValue = parseInt(this.playTotalTime/100);
		if(this.seekAddValue<1){this.seekAddValue = 1;}
		this.curTime = this.playCurTime;
		
		var seekBoxTip = document.getElementById('seekBoxTip');
		seekBoxTip.innerHTML = '&nbsp;';
		
		this.changeSeekPos();
		
		var seekBox = document.getElementById('seekBox');
		seekBox.style.display = 'block';
		document.focus('playSeekA');
		
		if(!bo){
			var date = new Date();
			KEY.PRESSTIME = date.getTime();		//
		}else{
			KEY.PRESSTIME =  0;
		}
		if(this.hideSeekTimer) {clearInterval(this.hideSeekTimer);this.hideSeekTimer = null;}
		this.hideSeekTimer = setInterval( "myPlayer.hideSeekAuto()",1*1000);
	},
	initSeekBox:function(){
		var a = document.getElementById('playSeekA');
		a.onfocus = function(){KEY.CURTYPE = 'seekBox';}
	},
	changeSeekPos:function(){
		var w = this.getSeekPosition(this.curTime);
		var seekBoxPos = document.getElementById('seekBoxPos'); 
		seekBoxPos.style.width= w;
		
		var seekTotalTimeBox = document.getElementById('seekTotalTimeBox');
		seekTotalTimeBox.innerHTML = this.formatPlayTime(this.curTime)+' / '+this.formatPlayTime(this.playTotalTime);
		
	},
	getSeekPosition:function(val){
		var w = val*this.seekBoxWidth/(this.playTotalTime - 0);
		return parseInt(w);
	},
	hideSeekAuto:function(){
		var date = new Date();
		var curTime = date.getTime();	
		if((curTime - KEY.PRESSTIME) >= 3000){
			if(this.oldPlayCurTime != this.curTime){
				this.setSeek();
			}else{
				var playerState = player.getPlayerState(); 
				if( playerState == 5){
					this.hideSeekBox();
				}else{
					var percent = player.getPlayerBuf() * 100 /(player.getPlayBufferTime () );
					var speed = 0;
					if(this.fileId){
						var info = rfpMgr.jsGetRfpInfo(this.fileId);
						speed = info.dnSpeed; //下载速度;
					}
					var seekBoxTip = document.getElementById('seekBoxTip');
					html = 'Download:'+ parseInt(percent)+'%&nbsp;&nbsp;';
					if(this.fileId){
						html +='Speed:'+$.formatSpaceKB(speed)+'/s';
					}
					seekBoxTip.innerHTML = html;
				}
			}
		}
	},
	hideSeekBox:function(){
		if(this.hideSeekTimer) {clearInterval(this.hideSeekTimer);this.hideSeekTimer = null;}
		KEY.CURTYPE = '';
		var seekBox = document.getElementById('seekBox');
		seekBox.style.display = 'none';
		
		this.isShowSeek = false;
	},
	changeSeekAdd:function(){
		
		if(this.curTime < this.playTotalTime){
			this.curTime +=this.seekAddValue;
			if(this.curTime > this.playTotalTime){
				this.curTime = this.playTotalTime;
			}
			this.changeSeekPos();
		}	
	},
	changeSeekDec:function(){
		if(this.curTime > 0){
			this.curTime -=this.seekAddValue;
			if(this.curTime <0){
				this.curTime = 0;
			}
			this.changeSeekPos();
		}
	},
	setSeekOk:function(){
		var percent = 0;
		var seekBoxTip = document.getElementById('seekBoxTip');
		seekBoxTip.innerHTML = 'Download:'+ parseInt(percent)+'%&nbsp;&nbsp;';
		this.setSeek();
	},
	setSeek:function(){
		player.seekPlay(this.curTime);
		this.oldPlayCurTime = this.curTime;
	},
	formatPlayTime:function(t){
		var h = parseInt(t/(60*60));
		
		var tm = t - 60*60*h ;
		var m = parseInt( tm/60);
		
		var s = tm - m*60;
		
		var con = '';
		if(h>0){
			if(h>9){
				con+=h+':';
			}else{
				con+='0'+h+':';
			}
		}
		
		if(m>9){
			con+=m+':';
		}else{
			con+='0'+m+':';
		}
		
		if(s>9){
			con+=s;
		}else{
			con+='0'+s;
		}
		
		return con;
	},
	hasInitContralBox:false,
	hideContralTimer:null,
	contralMap:{
		scalerRatio:{
			minValue:-12,
			maxValue:12,
			val:0,
			getRealValue:function(v){
				return 12 - v;
			},	
			setDisplay:function(v){
				
			},	
			name:'Scaler Ratio',
		},
		brightness:{
			minValue:-128,
			maxValue:127,
			val:$.getConfigNumberValue("display_brightness",0),		//显示亮度,
			getRealValue:function(v){
				return this.val;
			},	
			setDisplay:function(v){
				
			},	
			name:'Brightness',
		},
		contrast:{
			minValue:0,
			maxValue:255,
			val:$.getConfigNumberValue("display_contrast",128),
			getRealValue:function(){
				return this.val;
			},	
			setDisplay:function(v){
				this.val = v;
			},	
			name:'Contrast',
		},
	},
	contralBoxWidth:150,
	getContralItemWidth:function(obj){
		var w = parseInt((obj.val - obj.minValue)*this.contralBoxWidth/(obj.maxValue - obj.minValue));
		return w;
	},
	changeContralTime:function(){
		var cur = player.getCurTime ();
		var dur = player.getDuration();
		cur = parseInt(cur/1000);
		dur = parseInt(dur/1000);
		var con = this.formatPlayTime(cur)+'/'+this.formatPlayTime(dur);
		var contralTimeBox = document.getElementById('contralTimeBox');
		contralTimeBox.innerHTML = con;
	
	},
	showContralBox:function(){
		if(!this.hasInitContralBox){
			this.initContralBox();
			this.hasInitContralBox = true;
		}
		alert(this.disName);
			
		var contralTitle = document.getElementById('contralTitle');		
		contralTitle.innerHTML = this.disName;
		var contralBox = document.getElementById('contralBox');
		contralBox.style.display = 'block';
		var contralBoxList = document.getElementById('contralBoxList');
		var aList = contralBoxList.getElementsByTagName('a');
		aList[0].focus();
		
		var date = new Date();
		KEY.PRESSTIME = date.getTime();		//
		if(this.hideContralTimer) {clearInterval(this.hideContralTimer);this.hideContralTimer = null;}
		this.hideContralTimer = setInterval( "myPlayer.hideContralAuto()",1*1000);
	},
	hideContralAuto:function(){
		var date = new Date();
		var curTime = date.getTime();	
		this.changeContralTime();
		if((curTime - KEY.PRESSTIME) >= 10000){
			this.hideContralBox();
		}
		
	},
	changeContralDec:function(){
		var a = document.activeElement;
		var tar = a.getAttribute('tar');
		var obj = this.contralMap[tar];
		if(obj.val > obj.minValue){
			obj.val --;
			var w = this.getContralItemWidth(obj);
			var box = document.getElementById('contral-pos-'+tar);
			box.style.width = w;
			var val = document.getElementById('contral-val-'+tar);
			val.innerHTML = obj.val
		}
		if(tar == 'scalerRatio'){
			
			player.scalerRatio(myPlayer.contralMap.scalerRatio.getRealValue(obj.val))
		}else if(tar == 'brightness'){
			mBrsrScaler.setBrightness(myPlayer.contralMap.brightness.val);
		}else if(tar == 'contrast'){
			mBrsrScaler.setContrast(myPlayer.contralMap.contrast.val);
		}
	},
	changeContralAdd:function(){
		var a = document.activeElement;
		var tar = a.getAttribute('tar');
		var obj = this.contralMap[tar];
		if(obj.val < obj.maxValue){
			obj.val ++;
			var w = this.getContralItemWidth(obj);
			var box = document.getElementById('contral-pos-'+tar);
			box.style.width = w;
			var val = document.getElementById('contral-val-'+tar);
			val.innerHTML = obj.val ;
		}
		
		if(tar == 'scalerRatio'){
			player.scalerRatio(myPlayer.contralMap.scalerRatio.getRealValue(obj.val))
		}else if(tar == 'brightness'){
			mBrsrScaler.setBrightness(myPlayer.contralMap.brightness.val);
		}else if(tar == 'contrast'){
			mBrsrScaler.setContrast(myPlayer.contralMap.contrast.val);
		}
	},
	initContralBox:function(){
		var contralBoxList = document.getElementById('contralBoxList');
		var con = '';
		
		for(var m in this.contralMap){
			if(this.contralMap.hasOwnProperty(m)){
				var obj = this.contralMap[m];
				var w = this.getContralItemWidth(obj);
				con +='<div class="contralItem">';
					con += '<div class="contarlItemTip"><a href="#" tar="'+m+'">'+obj.name+':</a></div>';
					con += '<div class="contralItemBox2"><div class="contralItemBox"><div class="contralItemPos" id="contral-pos-'+m+'" style="width:'+w+'px;">&nbsp;</div></div></div>';
					con +='<div class="contarlItemVal" id="contral-val-'+m+'" >'+(obj.val)+'</div>';
				con+='</div>';
			}
		}
		contralBoxList.innerHTML = con;
		
		var aList = contralBoxList.getElementsByTagName('a');
		var a;
		for(var i = 0;i<aList.length;i++){
			a = aList[i];
			a.onfocus = this.focusBtn;
			a.onblur = this.blurBtn;
			a.onclick = this.clickBtn;
		}
	},
	
	focusBtn:function(event){
		var a = event.currentTarget;
		var par  = a.parentNode.parentNode;
		var tar = a.getAttribute('tar');
		par.className = 'contralCurItem';
		KEY.CURTYPE = 'contralBox';
	},
	blurBtn:function(event){
		var a = event.currentTarget;
		var par  = a.parentNode.parentNode;
		var tar = a.getAttribute('tar');
		par.className = 'contralItem';
	},
	clickBtn:function(event){
		
	},
	hideContralBox:function(){
		document.focus('playContralA');
		
		var contralBox = document.getElementById('contralBox');
		contralBox.style.display = 'none';
		KEY.CURTYPE = 'false';
		if(this.onHideContralBox){
			this.onHideContralBox();
		}
		if(this.hideContralTimer){
			clearInterval(this.hideContralTimer);
		}
	},
}



KEY.TYPE['volumeBox'] = keyRecvVolumeBox;

function keyRecvVolumeBox(key){
	
	if(key == 0x11){		//返回
		myPlayer.hideVolume();
		return false;
	}else if(key == 0xD || key== 0x1b){		//向右
		myPlayer.changeVolumeAdd();
		return false;
	}else if(key == 0xC || key== 0x1c){		//向左
		myPlayer.changeVolumeDec();;
		return false;
	}else if(key == 0x1a){	//静音
		myPlayer.changeVolumeMute();;
	}else if(key == 0x29 || key == 0x27){
		myPlayer.hideVolume();
		myPlayer.run('seek');
		return false;
	}else if(key ==0xE ){
		myPlayer.hideVolume();
		myPlayer.run('ok');
	}else if(key == 0xB || key == 0x1e){
		myPlayer.hideVolume();
		KEY.KEY_DOWN();
	}else if(key ==0xA || KEY== 0x1d){
		myPlayer.hideVolume();
		KEY.KEY_UP();
	}
	return false;
}

KEY.TYPE['seekBox'] = keyRecvSeekBox;

function keyRecvSeekBox(key){
	
	if(key == 0x11){		//返回
		myPlayer.hideSeekBox();
		return false;
	}else if(key==0x29){		//向右
		myPlayer.changeSeekAdd();
		return false;
	}else if( key == 0x27){		//向左
		myPlayer.changeSeekDec();;
		return false;
	}else if(key == 0xE){
		myPlayer.setSeekOk();
	}else if(key == 0xD || key == 0xC ||  key== 0x1b ||  key== 0x1c){
		myPlayer.hideSeekBox();
		myPlayer.run('left');
		return false;
	}else if(key == 0x1a){
		myPlayer.hideSeekBox();
		myPlayer.run('mute');
		return false;
	}else if(key ==0xE ){
		myPlayer.hideSeekBox();
		myPlayer.run('ok');
	}else if(key == 0xB || key == 0x1e){
		myPlayer.hideSeekBox();
		KEY.KEY_DOWN();
	}else if(key ==0xA || KEY== 0x1d){
		myPlayer.hideSeekBox();
		KEY.KEY_UP();
	}
	return true;
}

KEY.TYPE['contralBox'] = keyRecvContralBox;

function keyRecvContralBox(key){
	
	if(key == 0x11){		//返回
		myPlayer.hideContralBox();
		return false;
	}else if( key == 0xC){		//向左
		myPlayer.changeContralDec();
		return false;
	}else if(key==0xD){		//向右
		myPlayer.changeContralAdd();
		return false;
	}
	return true;
}

KEY.TYPE['pauseOrPlay'] = keyRecvPauseOrPlay;
function keyRecvPauseOrPlay(key,keyName){
	if(keyName == 'KEY_PLAY_STOP' || keyName == 'KEY_RETURN'){
		myPlayer.run('pause');
	}else if(keyName == 'KEY_PLAY_STOP'){
		myPlayer.run('pause');
	}else if(keyName == 'KEY_OK'){
		if(!myPlayer.hidePauseOrPlayTip){
			myPlayer.hidePauseOrPlayTipBox();
		}
	}
	return false;
}